/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.fdm.tst.security;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import javax.security.auth.callback.Callback;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.callback.NameCallback;
import javax.security.auth.callback.PasswordCallback;
import javax.security.auth.callback.UnsupportedCallbackException;

/**
 *
 * @author fdm
 */
public class CmdPromptLoginCallbackHandler implements CallbackHandler {

    @Override
    public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
        for(Callback cb : callbacks){
            if(cb instanceof NameCallback){
                System.out.print("login: ");
                BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
                String username = br.readLine();
                //String username = System.console().readLine("Username: ");
                ((NameCallback)cb).setName(username);
                
            } else if (cb instanceof PasswordCallback){
                //char[] password = System.console().readPassword("Password: ");
                System.out.print("password: ");
                BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
                String password = br.readLine();
                ((PasswordCallback)cb).setPassword(password.toCharArray());
                
            } else {
                throw new UnsupportedCallbackException(cb);
            }
        }
    }
    
}
